//
//  HomePendingCell.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/7/27.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "HomePendingCell.h"
#import "HomeMenuView.h"

@interface HomePendingCell () <UIScrollViewDelegate>

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIImageView *logoImgView;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *countLab;
@property (nonatomic, strong) UILabel *descLab;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;

@end

@implementation HomePendingCell

+ (instancetype)HomePendingCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"HomePendingCell";
    HomePendingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[HomePendingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
//        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, ScreenW - 20, 233)];
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, ScreenW - 20, 146)];
        self.bgView.backgroundColor = HexColor(0xffffff);
        self.bgView.layer.masksToBounds = YES;
        self.bgView.layer.cornerRadius = 6;
        [self.contentView addSubview:self.bgView];
        
        self.logoImgView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 17, 18, 18)];
        self.logoImgView.image = [UIImage imageNamed:@"icon_pending"];
        [self.bgView addSubview:self.logoImgView];
        
        self.titleLab = [[UILabel alloc] initWithFrame:CGRectMake(self.logoImgView.nim_right + 8, 17.5, [FactoryMethod sizeWithText:@"待处理" font:BoldFontOfSize(16) maxSize:CGSizeMake(0, 0)].width, 17)];
        self.titleLab.textColor = HexColor(0x2B2C44);
        self.titleLab.font = BoldFontOfSize(16);
        self.titleLab.text = @"待处理";
        [self.bgView addSubview:self.titleLab];
        
        self.countLab = [UILabel new];
        self.countLab.textColor = HexColor(0x9C9DB3);
        self.countLab.font = [UIFont systemFontOfSize:12];
        [self.bgView addSubview:self.countLab];
        
        self.descLab = [[UILabel alloc] initWithFrame:CGRectMake(self.bgView.nim_width - 10 - 140, 19.5, 140, 13)];
        self.descLab.textColor = HexColor(0x9C9DB3);
        self.descLab.textAlignment = NSTextAlignmentRight;
        self.descLab.font = [UIFont systemFontOfSize:12];
        self.descLab.text = @"等待您处理的事项";
        [self.bgView addSubview:self.descLab];
        
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(15, self.logoImgView.nim_bottom + 10, self.bgView.nim_width - 30, 84)];
        self.scrollView.backgroundColor = [UIColor whiteColor];
        self.scrollView.bounces = YES;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.pagingEnabled = YES;
        self.scrollView.delegate = self;
        [self.bgView addSubview:self.scrollView];
        
        self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(self.scrollView.nim_left, self.scrollView.nim_bottom + 5, self.scrollView.nim_width, 5)];
        self.pageControl.pageIndicatorTintColor = HexColor(0xC3C4CC);
        self.pageControl.currentPageIndicatorTintColor = Color_ThemeColor;
//        if (@available(iOS 14.0, *)) {
//            self.pageControl.preferredIndicatorImage = [UIImage imageNamed:@"home_pagecontrol_selected_img"];
//        } else {
//            // Fallback on earlier versions
//        }
        [self.bgView addSubview:self.pageControl];
        
        self.height = self.bgView.nim_bottom;
    }
    return self;
}

//-(void)setPendingArray:(NSArray<HomeMenuModel *> *)pendingArray {
//    _pendingArray = pendingArray;
//    CGFloat margin = (self.scrollView.nim_width - 84 * 3) * 0.5;
////    long i = pendingArray.count / 2 + ((pendingArray.count % 2) > 0 ? 1 : 0);
//    self.scrollView.contentSize = CGSizeMake(self.bgView.nim_width - 30, 84);
//    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
//    __weak_self;
//    [pendingArray enumerateObjectsUsingBlock:^(HomeMenuModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        HomeMenuView *view = [[HomeMenuView alloc] initWithFrame:CGRectMake(84 * idx + margin * idx, 0, 84, 84)];
//        view.menuModel = obj;
//        view.clickBlock = ^{
//            if (idx == 0 || idx == 1) {
//                //受理、回单确认
//                if (weakself.woFaYunBlock) {
//                    weakself.woFaYunBlock();
//                }
//            }else {
//                //运单签收
//                if (weakself.woShouBlock) {
//                    weakself.woShouBlock();
//                }
//            }
//        };
//        [weakself.scrollView addSubview:view];
//    }];
//}

//-(void)setTotal_accept:(NSInteger)total_accept {
//    _total_accept = total_accept;
//    self.countLab.text = [NSString stringWithFormat:@"(%ld)",(long)total_accept];
//    [self.countLab sizeToFit];
//    self.countLab.nim_left = self.titleLab.nim_right + 5;
//    self.countLab.nim_centerY = self.titleLab.nim_centerY;
//}

-(void)setPendingArray:(NSArray<HomeMenuModel *> *)pendingArray {
    _pendingArray = pendingArray;
//    if (pendingArray.count) {
//        long allPage = ((pendingArray.count - 1) / 8) + 1;
//        self.pageControl.numberOfPages = allPage;
//        if (allPage > 1) {
//            self.pageControl.hidden = NO;
//        }else {
//            self.pageControl.hidden = YES;
//        }
//        CGFloat viewW = 78;
//        CGFloat margin = (self.scrollView.nim_width - viewW * 4) * 0.333333;
//        self.scrollView.contentSize = CGSizeMake(self.scrollView.nim_width * allPage, viewW);
//        [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
//
//        for (int i = 0; i < allPage; i++) {
//            UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(i * self.scrollView.nim_width, 0, self.scrollView.nim_width, viewW)];
//            bgView.backgroundColor = HexColor(0xffffff);
//            __weak_self;
//            [pendingArray enumerateObjectsUsingBlock:^(HomeMenuModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//                if (idx / 8 == i) {
//                    HomeMenuView *view = [[HomeMenuView alloc] initWithFrame:CGRectMake(viewW * (idx % 4) + margin * (idx % 4), (viewW + 10) * ((idx % 8) / 4), viewW, viewW)];
//                    view.menuModel = obj;
//                    view.clickBlock = ^{
//                        if (obj.type == HomeMenuTypeTaskSure) {
//                            if (weakself.taskSureBlock) {
//                                weakself.taskSureBlock();
//                            }
//                        }else if (obj.type == HomeMenuTypeReceiveGoods) {
//                            if (weakself.jieShouHuoWuBlock) {
//                                weakself.jieShouHuoWuBlock();
//                            }
//                        }else if (obj.type == HomeMenuTypeCarDiaoDu) {
//                            if (weakself.carDiaoDuBlock) {
//                                weakself.carDiaoDuBlock();
//                            }
//                        }else if (obj.type == HomeMenuTypeHuiDanBack) {
//                            if (weakself.huiDanFanHuiBlock) {
//                                weakself.huiDanFanHuiBlock();
//                            }
//                        }else if (obj.type == HomeMenuType3PLInvite) {
//                            if (weakself.huoZhuYaoQingBlock) {
//                                weakself.huoZhuYaoQingBlock();
//                            }
//                        }else if (obj.type == HomeMenuTypeYunCarOwnerInvite) {
//                            if (weakself.siJiYaoQingBlock) {
//                                weakself.siJiYaoQingBlock();
//                            }
//                        }
//                    };
//                    [bgView addSubview:view];
//                }
//            }];
//            [self.scrollView addSubview:bgView];
//        }
//    }

    if (pendingArray.count) {
        long allPage = ((pendingArray.count - 1) / 4) + 1;
        self.pageControl.numberOfPages = allPage;
        if (allPage > 1) {
            self.pageControl.hidden = NO;
        }else {
            self.pageControl.hidden = YES;
        }
        CGFloat viewW = 78;
        CGFloat margin = (self.scrollView.nim_width - viewW * 4) * 0.333333;
        self.scrollView.contentSize = CGSizeMake(self.scrollView.nim_width * allPage, viewW);
        [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        
        for (int i = 0; i < allPage; i++) {
            UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(i * self.scrollView.nim_width, (self.scrollView.nim_height - viewW) * 0.5, self.scrollView.nim_width, viewW)];
            bgView.backgroundColor = HexColor(0xffffff);
            __weak_self;
            [pendingArray enumerateObjectsUsingBlock:^(HomeMenuModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if (idx / 4 == i) {
                    HomeMenuView *view = [[HomeMenuView alloc] initWithFrame:CGRectMake(viewW * (idx % 4) + margin * (idx % 4), 0, viewW, viewW)];
                    view.menuModel = obj;
                    view.clickBlock = ^{
                        if (obj.type == HomeMenuTypeTaskSure) {
                            if (weakself.taskSureBlock) {
                                weakself.taskSureBlock();
                            }
                        }else if (obj.type == HomeMenuTypeReceiveGoods) {
                            if (weakself.jieShouHuoWuBlock) {
                                weakself.jieShouHuoWuBlock();
                            }
                        }else if (obj.type == HomeMenuTypeCarDiaoDu) {
                            if (weakself.carDiaoDuBlock) {
                                weakself.carDiaoDuBlock();
                            }
                        }else if (obj.type == HomeMenuTypeHuiDanBack) {
                            if (weakself.huiDanFanHuiBlock) {
                                weakself.huiDanFanHuiBlock();
                            }
                        }else if (obj.type == HomeMenuType3PLInvite) {
                            if (weakself.huoZhuYaoQingBlock) {
                                weakself.huoZhuYaoQingBlock();
                            }
                        }else if (obj.type == HomeMenuTypeYunCarOwnerInvite) {
                            if (weakself.siJiYaoQingBlock) {
                                weakself.siJiYaoQingBlock();
                            }
                        }
                    };
                    [bgView addSubview:view];
                }
            }];
            [self.scrollView addSubview:bgView];
        }
    }
    
    
//    CGFloat viewW = 84;
//    CGFloat margin = (self.scrollView.nim_width - viewW * 3) * 0.5;
////    long i = pendingArray.count / 2 + ((pendingArray.count % 2) > 0 ? 1 : 0);
//    self.scrollView.contentSize = CGSizeMake(viewW * 3 + margin * 2, viewW * 2);
//    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
//    __block CGFloat viewX = 0;
//    __weak_self;
//    [pendingArray enumerateObjectsUsingBlock:^(HomeMenuModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        HomeMenuView *view = [[HomeMenuView alloc] initWithFrame:CGRectMake(viewX, viewW * (idx / 3), viewW, viewW)];
//        view.menuModel = obj;
//        view.clickBlock = ^{
//            if ([User userIsDriver]) {
//                if (idx == 0) {
//                    if (self.taskSureBlock) {
//                        self.taskSureBlock();
//                    }
//                }else if (idx == 1) {
//                    if (self.jieShouHuoWuBlock) {
//                        self.jieShouHuoWuBlock();
//                    }
//                }else if (idx == 2) {
//                    if (self.huiDanFanHuiBlock) {
//                        self.huiDanFanHuiBlock();
//                    }
//                }else if (idx == 3) {
//                    if (self.huoZhuYaoQingBlock) {
//                        self.huoZhuYaoQingBlock();
//                    }
//                }else if (idx == 4) {
//                    if (self.siJiYaoQingBlock) {
//                        self.siJiYaoQingBlock();
//                    }
//                }
//            }else {
//                if (idx == 0) {
//                    if (self.taskSureBlock) {
//                        self.taskSureBlock();
//                    }
//                }else if (idx == 1) {
//                    if (self.jieShouHuoWuBlock) {
//                        self.jieShouHuoWuBlock();
//                    }
//                }else if (idx == 2) {
//                    if (self.carDiaoDuBlock) {
//                        self.carDiaoDuBlock();
//                    }
//                }else if (idx == 3) {
//                    if (self.huiDanFanHuiBlock) {
//                        self.huiDanFanHuiBlock();
//                    }
//                }else if (idx == 4) {
//                    if (self.huoZhuYaoQingBlock) {
//                        self.huoZhuYaoQingBlock();
//                    }
//                }
//            }
//        };
//        [weakself.scrollView addSubview:view];
//        viewX = view.nim_right + margin;
//        if (viewX >= viewW * 3 + margin * 2 - 1) {
//            viewX = 0;
//        }
//    }];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    self.pageControl.currentPage = self.scrollView.contentOffset.x / self.scrollView.nim_width;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
